package cn.stard.pet.controller;

import cn.stard.basic.annotation.PreAuthorize;
import cn.stard.basic.exception.BusinessException;
import cn.stard.basic.util.LoginContext;
import cn.stard.org.domain.Employee;
import cn.stard.org.service.IEmployeeservice;
import cn.stard.pet.domain.HandlerMsgDto;
import cn.stard.pet.service.ISearchMasterMsgService;
import cn.stard.pet.domain.SearchMasterMsg;
import cn.stard.pet.query.SearchMasterMsgQuery;
import cn.stard.basic.util.AjaxResult;
import cn.stard.basic.util.PageList;
import cn.stard.user.domain.Logininfo;
import cn.stard.user.domain.User;
import cn.stard.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IEmployeeservice employeeservice;
    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryData(query);
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest req){
        try {
            // 1.给寻主消息的userId设置值
            Logininfo logininfo = LoginContext.getLogininfo(req);
            User user = userService.loadBylogininfo(logininfo.getId());
            searchMasterMsg.setUserId(user.getId());
            // 2.调用业务方法进行寻主消息发布
            searchMasterMsgService.publicsh(searchMasterMsg);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    // 2.后台平台管理员查询待审核
    // 待审核：state=0
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req)
    {
        // 1.设置查询状态为审核通过
        query.setState(1);
        // 2.设置当前查询用户对应的店铺Id
        Logininfo logininfo = LoginContext.getLogininfo(req);
        // 获取当前登录人的信息
        Employee employee = employeeservice.loadByloginfoId(logininfo.getId());
        // 把当前登录人的shopid添加到query里面
        query.setShopId(employee.getShopId());
        return searchMasterMsgService.queryData(query);
    }
    /* 
    * @Title: 
    * @Description: 店铺员工接单
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:     
    * @Parameters: 
    * @Return 
    */
// 1.前台用户查询寻主列表
    // 所有状态都查询出来
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        // 1.查询出user用户
        Logininfo logininfo = LoginContext.getLogininfo(req);
        User user = userService.loadBylogininfo(logininfo.getId());
        query.setUserId(user.getId());
        return searchMasterMsgService.queryData(query);
    }


    // 2.后台平台管理员查询待审核
    // 待审核：state=0
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        // 1.查询出user用户
        query.setState(0);
        return searchMasterMsgService.queryData(query);
    }
    // 4.平台管理员和店铺店长都可以看到寻主池的消息
    // 待处理：state=1&shopId=null
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        // 1.设置查询状态为审核通过
        query.setState(1);
        return searchMasterMsgService.queryData(query);
    }

    // 4.平台管理员和店铺店长都可以已完成的数据
    // 待处理：state=2
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        // 1.设置查询状态为审核通过
        query.setState(2);
        return searchMasterMsgService.queryData(query);
    }

    @PostMapping("/accept")
    public AjaxResult  accept(@RequestBody HandlerMsgDto handlerMsgDto)
    {
        try {
            // 根据店铺员工接单传入的参数调用方法
            searchMasterMsgService.accept(handlerMsgDto);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}


